﻿using System;
using System.Net;

namespace HZY.Toolkit
{
    /// <summary>
    /// MVC 异常拦截 该对象 返回客户端
    /// </summary>
    [Serializable]
    public class MessageBox : Exception
    {
        /// <summary>
        /// 异常模型
        /// </summary>
        public ApiResult Model { set; get; }

        /// <summary>
        /// 成功消息 | 客户端接收对象 例如: {StatusCode=1,Value="操作成功!"}
        /// </summary>
        public MessageBox()
            : base("操作成功!")
        {
            this.Model = new ApiResult((int)HttpStatusCode.OK, "操作成功!");
        }

        /// <summary>
        /// 失败并返回 消息 | 客户端接收对象 例如: {StatusCode=0,Value="这是消息!"}
        /// </summary>
        /// <param name="Messager"></param>
        public MessageBox(string Message)
            : base(Message)
        {
            this.Model = new ApiResult((int)HttpStatusCode.BadRequest, Message);
        }

        /// <summary>
        /// 自定义异常
        /// </summary>
        /// <param name="Data">客户端接收对象 例如: {StatusCode=1,Value="这是消息!"}</param>
        public MessageBox(object Message, int StatusCode)
            : base($"自定义消息!")
        {
            this.Model = new ApiResult(StatusCode, Message);
        }

        public MessageBox(string message, Exception innerException)
            : base(message, innerException)
        {

        }

        protected MessageBox(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {

        }
    }
    //        错误的请求结果 BadRequestResult 400
    //冲突结果 ConflictResult 409
    //没有内容结果 NoContentResult 204
    //没有找到结果 NotFoundResult 404
    //好的结果 OkResult 200
    //未经授权的结果 UnauthorizedResult 401
    //不可处理的实体结果 UnprocessableEntityResult 422
    //不支持的媒体类型结果 UnsupportedMediaTypeResult 415
    //内部服务器错误结果 InternalServerErrorResult 500
    public class ApiResult
    {
        public ApiResult(int statusCode, object value)
        {
            this.StatusCode = statusCode;
            this.Value = value;
        }

        public int StatusCode { get; set; } = (int)HttpStatusCode.OK;

        public object Value { get; set; }

    }




}
